import RequestProcess from "./RequestProcess";
import operator from "../operator/Operator";
import OperateArgs from "../operator/OperateArgs";
import OperatorDecorator from "../operator/OperatorDecorator";
import CheckUserIdExistOperator from "../operator/CheckUserIdExistOperator";
import CheckGroupIdExistOperator from "../operator/CheckGroupIdExistOperator";
import EnterGroupOperator from "../operator/EnterGroupOperator";
import { RC } from "../config/RC";


class EnterGroupRequestProcess extends RequestProcess {

    public args_generate(): void {
        this.args = new OperateArgs();
        this.args.setProperty("userId", this.req.body.userId);
        this.args.setProperty("groupId", this.req.body.groupId);
    }

    public operator_plan_generate(): void {
        const check_userId_exist_operator = new CheckUserIdExistOperator(this.args);
        const check_groupId_exist_operator = new CheckGroupIdExistOperator();
        const enter_group_operator = new EnterGroupOperator();

        this.operator_plan = new OperatorDecorator(check_groupId_exist_operator, check_userId_exist_operator);
        this.operator_plan = new OperatorDecorator(enter_group_operator, this.operator_plan);
    }

    public writeResult(rc: RC): Object {
        switch (rc) {
            case RC.SUCCESS:
                return {
                    code: "0000",
                    msg: "加入群组成功",
                    groupId: this.getOperateArgs().getProperty("generateId")
                }
            case RC.USERID_NOTEXIST:
                return {
                    code: "1004",
                    msg: "用户ID不存在"
                }
            case RC.GROUPID_NOTEXIST:
                return {
                    code: "1008",
                    msg: "群组ID不存在"
                }
            case RC.INVALID_ARGUMENT:
                return {
                    code: "1002",
                    msg: "参数不合法"
                }
            case RC.INTERNAL_ERROR:
                return {
                    code: "1003",
                    msg: "服务器内部错误"
                }
            default:
                return {
                    code: "2000",
                    msg: "未知错误"
                }
        }
    }
}

export default EnterGroupRequestProcess;